#include "outwidget.h"

OutWidget::OutWidget(QWidget *parent) : QWidget(parent)
{
    this->init_Widget();
    this->init_Control();
    this->init_Connect();
}

void OutWidget::init_Control()
{
    this->returnBtn=new QPushButton();
    this->returnBtn->setText(QString::fromUtf8("返回"));

    this->ensureBtn=new QPushButton();
    this->ensureBtn->setText(QString::fromUtf8("确认"));

    this->things_idLab=new QLabel();
    this->things_idLab->setText(QString::fromUtf8("物品id："));
    this->things_numberLab=new QLabel();
    this->things_numberLab->setText(QString::fromUtf8("出库量："));

     this->things_idEdit=new QLineEdit();
     this->things_numberEdit=new QLineEdit();

    this->BtnLay=new QHBoxLayout();
    this->BtnLay->addWidget(this->returnBtn);
    this->BtnLay->addWidget(this->ensureBtn);

    this->Leay=new QHBoxLayout();
    this->Leay->addWidget(this->things_numberLab);
    this->Leay->addWidget(this->things_numberEdit);

    this->L_eLay=new QHBoxLayout();
    this->L_eLay->addWidget(this->things_idLab);
    this->L_eLay->addWidget(this->things_idEdit);

    this->Lay=new QVBoxLayout();
    this->Lay->addLayout(this->L_eLay);
    this->Lay->addLayout(this->Leay);
    this->Lay->addLayout(this->BtnLay);

    this->setLayout(this->Lay);
}

void OutWidget::init_Widget()
{
    this->setWindowTitle(QString::fromUtf8("用户界面"));
    this->resize(800,600);
}

void OutWidget::init_Connect()
{
    connect(this->ensureBtn,SIGNAL(clicked()),this,SLOT(on_ensureBtn_cliked()));
    connect(this->returnBtn,SIGNAL(clicked()),this,SLOT(on_retutrnBtn_cliked()));
}

void OutWidget::doStockOut()
{
    //确认id是否存在
    QString id,num;int Ture;
    id=this->things_idEdit->text();
    num=this->things_numberEdit->text();
    QString sql=QString("SELECT *FROM StockRecord WHERE tings_id=%1;").arg(id);
    this->model=this->control->getInstance()->Creatmodel("wuping");
    stock=this->model->getStockByName((char*)sql.toStdString().c_str());
    //qDebug()<<stock->getTings_count();
    if(stock!=nullptr)
    {
        qDebug()<<"stock";
        //如果库存数量大于出库数量则可以出库
        if(stock->getTings_count()+48>num)
        {
            sql=QString("UPDATE StockRecord SET tings_count=tings_count-%1 WHERE tings_id=%2;").arg(num).arg(id);
            Ture=this->model->Table_Inserter((char*)sql.toStdString().c_str());
            if(Ture)
            {

                 QMessageBox::information(this,"成功","出库成功");
                 this->things_idEdit->clear();
                 this->things_numberEdit->clear();

                 this->model=this->control->getInstance()->Creatmodel("inout");
                 sql=QString("INSERT INTO InOut_Stock_Table(user_id,tings_id,do_type,number) VALUES (%1,%2,1,%3);").arg(stock->getUser_id()).arg(id).arg(num);
                 this->model->Table_Inserter((char*)sql.toStdString().c_str());

                 emit returnManager();

            }
            else
            {
                QMessageBox::critical(this, "错误", "出库失败");
                this->things_idEdit->clear();
               this->things_numberEdit->clear();
            }

        }
        //如果库存数与出库数相同则删除库存记录
      else if (stock->getTings_count()+48==num)
        {
            sql=QString("DELETE FROM StockRecord WHERE tings_id=%1;").arg(id);
            this->model->Table_Delete((char*)sql.toStdString().c_str());
            QMessageBox::information(this,"成功","出库成功");

            this->model=this->control->getInstance()->Creatmodel("inout");
            sql=QString("INSERT INTO InOut_Stock_Table(user_id,tings_id,do_type,number) VALUES (%1,%2,1,%3);").arg(id).arg(stock->getTings_id()).arg(num);
            this->model->Table_Inserter((char*)sql.toStdString().c_str());
        }
        else
        {
             QMessageBox::critical(this, "错误", "没有这么多货物");
        }
    }
    else
    {
       QMessageBox::critical(this, "错误", "没有该物品");
    }
}

void OutWidget::on_retutrnBtn_cliked()
{
     emit returnManager();
}

void OutWidget::on_ensureBtn_cliked()
{
    doStockOut();
}
